package com.huluobo.a2208mdmdemo.view

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2208mdmdemo.MyNavHost
import com.huluobo.a2208mdmdemo.R
import com.huluobo.a2208mdmdemo.demo.demo06

/**
 *  Created by LC on 2025/1/9.
 */
sealed class NavItem(val route: String, val title: String, val img: Int) {
    object messagePage : NavItem("message", "消息", R.drawable.icon1275)
    object homePage : NavItem("home", "门户", R.drawable.icon64)
    object minePage : NavItem("mine", "我的", R.drawable.icon64)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun mainPage() {
    val list = listOf<NavItem>(NavItem.messagePage, NavItem.homePage, NavItem.minePage)
    val navController = rememberNavController()
    //获取当前回退站中的所有页面的状态
    val stackEntryState by navController.currentBackStackEntryAsState()
    //所有导航的地址
    val destination = stackEntryState?.destination
    //脚手架,用于搭建页面的框架,它拥有头布局,脚布局(底部导航),浮动按钮之类的框架内容
    Scaffold(bottomBar = {
        if (destination?.hierarchy?.any {
                //通过拿到底部导航的地址,对比如果符合底部导航的地址,就展示NavigationBar,否则就不展示
                list.map { navItem -> navItem.route }.contains(it.route)
            } == true) {
            //底部导航,直接创建并没有东西,需要类似menu的内容
            NavigationBar {
                list.forEach {
                    //底部导航单一的item
                    NavigationBarItem(selected = false, onClick = {
                        //当点击时,跳转对应的地址
                        navController.navigate(it.route)
                    }, icon = {
                        //图标
                        Image(painter = painterResource(id = it.img), contentDescription = "")
                    }, label = {
                        //标签
                        Text(text = it.title)
                    })
                }
            }
        }

    }) {
        MyNavHost(it = it, navController = navController)
    }
}